package cn.edu.szu;

import java.io.FileWriter;
import java.io.IOException;

public class Writer extends myThread implements Runnable{

    // 指示当前id
    private final int id;

    // 构造方法
    public Writer(int id) {
        this.id = id;
    }

    @Override
    public void run() {
            P(rw_mutex);

            // It starts writing
            write();

            V(rw_mutex);
    }

    private void write() {

        printLog("No." + id + " is writing" + "\n");
        // 将字符串写入文件
        try {
            FileWriter writer = new FileWriter(file, true);
            writer.write(" No." + id);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
